package com.classtest.classtestdemo.controller;

import com.alibaba.fastjson.JSON;
import com.classtest.classtestdemo.bean.ImageTest;
import com.classtest.classtestdemo.service.ImageTestService;
import com.classtest.classtestdemo.utlis.PKGeneration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/file")
public class ImageTestController {
    @Autowired
    ImageTestService imageTestService;
    @RequestMapping(value = "/imageupload")
    public String imageUpload( @RequestParam("file") MultipartFile file){
        Map<String,String> result = new HashMap<String,String>();
        String resFlag = null;
        try{
            ImageTest imageTest = new ImageTest();
            imageTest.setId(PKGeneration.INSTANCE.nextId());
            imageTestService.imageUpload(JSON.toJSONString(imageTest),file);
        }catch (Exception e){
            result.put("error",e.toString());
            System.out.println(e.toString());
        }
        return JSON.toJSONString(result);
    }
}
